<template>
  <div class="search-result">
    <van-list v-model="loading"
              :finished="finished"
              finished-text="没有更多了"
              :error.sync="error"
              error-text="加载失败， 请点击重试！"
              @load="onLoad">
      <van-cell v-for="(article, index) in list"
                :key="index"
                :title="article.title" />
    </van-list>
  </div>

</template>

<script>
import { getSearchResult } from '@/api/search.js'
export default {
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20,
      error: false
    }
  },
  methods: {
    async onLoad () {
      const { data } = await getSearchResult({
        page: this.page,
        per_page: this.perPage,
        q: this.searchText

      })
      const { results } = data.data
      this.list.push(...results)
      this.loading = false
      if (results.length) {
        this.page++
      } else {
        this.finished = true
        this.loading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
// /deep/ .search-result {
//   /deep/ .van-list {
//     padding-top: 100px !important;
//   }
// }
</style>
